//
//  main.m
//  Province-City-zone
//
//  Created by dllo on 16/7/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "Distric.h"
#import "TimeCity.h"
int main(int argc, const char * argv[]) {
        NSString *path = @"/Users/dllo/Desktop/Province-City-zone/Province-City-zone/area.txt";
        NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
        NSMutableArray *allProvinceArray = [NSMutableArray array];
#if 0
        for (NSString *area in allArray) {
            if (![area hasPrefix:@" "]) {
                // 创建 省字典(name 和 市array)
                NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
                NSMutableArray *cityArr = [NSMutableArray array];
                [proDic setObject:area forKey:@"provinceName"];
                [proDic setObject:cityArr forKey:@"cityGroup"];
                [allProvinceArray addObject:proDic];
            } else if (![area hasPrefix:@"   "]) {
                 // 创建 市字典(name 和 区array)
                NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                NSMutableArray *districArr = [NSMutableArray array];
                [cityDic setObject:area forKey:@"cityName"];
                [cityDic setObject:districArr forKey:@"districtGroup"];
                [[[allProvinceArray lastObject] objectForKey:@"cityGroup"] addObject:cityDic];
            } else {
                // 最终将区添加到数组中
                [[[[[allProvinceArray lastObject] objectForKey:@"cityGroup"] lastObject] objectForKey:@"districtGroup"] addObject:area];
            }
        }
        NSLog(@"%@",allProvinceArray);
#endif
#if 0
        for (NSString *area in allArray) {
            if (![area hasPrefix:@" "]) {
                Province *province = [[Province alloc] initWithName:area];

                [allProvinceArray addObject:province];
            } else if (![area hasPrefix:@"   "]) {
                City *city = [[City alloc] initWithName:area];

                Province *province = [allProvinceArray lastObject];
                [province.arr addObject:city];
            } else {
                Distric *distric = [[Distric alloc] initWithName:area];
                Province *province = [allProvinceArray lastObject];
                City *city = [province.arr lastObject];
                [city.arr addObject:distric];

            }
        }
        NSLog(@"%@",allProvinceArray);
#endif
#if 0
//        NSString *locationPath = @"/Users/dllo/Desktop/Province-City-zone/Province-City-zone/Location1.txt";
//        NSString *source = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
//        NSArray *allLocationArray = [source componentsSeparatedByString:@","];
//        NSMutableArray *allCityDicArray = [NSMutableArray array];
//        for (NSString *city in allLocationArray) {
//             // 根据每个城市的|处理字符串,重新生成字典的数组
//            NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
//            NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName",[arrayCity lastObject], @"cityNumber", nil];
//            [allCityDicArray addObject:cityDic];
//        }
//        // NSSortDescriptor 可以直接用在数组上,也可以用在(数组套字典的键值对"key"或者"value"上排序,非常方便)
//        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
//        [allCityDicArray sortUsingDescriptors:@[sort]];
//
//        NSMutableArray *fiallyArray = [NSMutableArray array];
//        for (NSMutableDictionary *cityDic in allCityDicArray) {
//            NSString *cityNumber = cityDic[@"cityNumber"];
//            if ([cityNumber hasSuffix:@"0000"]) {
//                // 以自身字典生成新的字典,不是直接赋值,而是开辟新的空间初始化
//                NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
//                // 添加新的键值对,放市组
//                [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
//                [fiallyArray addObject:proDic];
//            } else if ([cityNumber hasSuffix:@"00"]) {
//                NSMutableArray *cityArray = [[fiallyArray lastObject] objectForKey:@"cityArray"];
//                NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
//                [dicCity setObject:[NSMutableArray array] forKey:@"districArray"];
//                [cityArray addObject:dicCity];
//            } else {
//                // 注意代码尽量分开写
//                NSMutableDictionary *proDic = [fiallyArray lastObject];
//                NSMutableArray *cityArray = proDic[@"cityArray"];
//                NSMutableDictionary *dicCity = [cityArray lastObject];
//                NSMutableArray *districArray = dicCity[@"districArray"];
//                NSMutableDictionary *districDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
//                [districArray addObject:districDic];
//            }
//        }
//        NSLog(@"%@",fiallyArray);
#endif
        //  NSSortDescriptor 的用法
//        NSArray *arr = @[@{@"name":@"x"}, @{@"name":@"p"}, @{@"name":@"y"}, @{@"name":@"a"}, @{@"name":@"q"}];
//        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
//        NSArray *newArr = [arr sortedArrayUsingDescriptors:@[sort]];
//        NSLog(@"%@",newArr);
        
        // 可变
        
//        NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"1", @"10",@"8", @"32", @"5", @"36", nil];
//        NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
//        [arr1 sortUsingDescriptors:@[sort1]];
//        NSLog(@"%@",arr1);
        // 不可变(有毒)
        
//        NSArray *arr = @[@"1", @"21", @"31", @"14"];
//        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
//        arr = [arr sortedArrayUsingDescriptors:@[sortDescriptor]];
//        NSLog(@"%@",arr);
        
        
        // 数组模型
        NSString *locationPath = @"/Users/dllo/Desktop/Province-City-zone/Province-City-zone/Location1.txt";
        NSString *source = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
        NSArray *allLocationArray = [source componentsSeparatedByString:@","];
//        NSLog(@"%@",allLocationArray);
        NSMutableArray *allCityDicArray = [NSMutableArray array];
        for (NSString *cityString in allLocationArray) {
            NSArray *city = [cityString componentsSeparatedByString:@"|"];
            // firstObject 对数组越界醉了优化,不会报错只会返回空值,比array[0]好
            TimeCity *timeCity = [[TimeCity alloc] initWithName:[city firstObject] cityNumber:[city lastObject]];
            [allCityDicArray addObject:timeCity];
        }
        
        // 排序
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [allCityDicArray sortUsingDescriptors:@[sortDescriptor]];
        
        NSMutableArray *finallArray = [NSMutableArray array];

        for (TimeCity *timeCity in allCityDicArray) {
            if ([timeCity.cityNumber hasSuffix:@"0000"]) {
                Province *pro = [Province districWithTimeCity:timeCity];
                [finallArray addObject:pro];
            } else if ([timeCity.cityNumber hasSuffix:@"00"]) {
                Province *pro = [finallArray lastObject];
                City *city = [City districWithTimeCity:timeCity];
                [pro.arr addObject:city];
            } else {
                Province *pro = [finallArray lastObject];
                City *city = [pro.arr lastObject];
                Distric *distrc = [Distric districWithTimeCity:timeCity];
                [city.arr addObject:distrc];
            }
        }
        NSLog(@"%@",finallArray);
    


    
    return 0;
}
